<template>
  <div ref="listBoxRef" class="article-list">
    <van-pull-refresh
      v-model="isreFreshLoading"
      :success-text="refreshSuccessText"
      :success-duration="1500"
      @refresh="onRefresh"
    >
      <van-list
        v-model="loading"
        :finished="finished"
        finished-text="没有更多了"
        :error.sync="error"
        error-text="请求失败，点击重新加载"
        @load="onLoad"
      >
        <ArticleItem
          v-for="(article, index) in list"
          :key="index"
          :article="article"
        >
        </ArticleItem>
        <!-- <van-cell
          v-for="(article, index) in list"
          :key="index"
          :title="article.title"
        /> -->
      </van-list>
    </van-pull-refresh>
  </div>
</template>

<script>
import { getArticlesAPI } from '@/api'
import ArticleItem from '@/components/article-item'
export default {
  name: 'ArticleList',
  data () {
    return {
      list: [], // 存储列表数据的数组
      loading: false, // 控制加载中loading状态
      finished: false, // 控制数据加载结束的状态
      timestamp: null, // 请求获取下一页数据的时间戳
      error: false, // 控制列表加载失败的提示状态
      isreFreshLoading: false, // 控制下拉刷新的loading状态
      refreshSuccessText: '刷新成功' // 下拉刷新成功提示文本
    }
  },
  components: {
    ArticleItem
  },
  props: {
    channel: {
      type: Object,
      required: true
    }
  },
  activated () {
    this.$refs.listBoxRef.scrollTop = localStorage.scrollTop || 0
    console.log('activated')
  },
  deactivated () {
    console.log('deactivated')
  },
  mounted () {
    let timer = null
    this.$refs.listBoxRef.onscroll = function (e) {
      // console.log(e)
      clearTimeout(timer)
      timer = setTimeout(() => {
        console.log(e.target.scrollTop)
        localStorage.scrollTop = e.target.scrollTop
      }, 50)
    }
  },
  methods: {
    async onLoad () {
      // 1.请求获取数据
      try {
        const { data } = await getArticlesAPI({
          channel_id: this.channel.id, // 频道id
          // 请求数据的页码
          // 请求第一页数据：当前最新时间戳
          timestamp: this.timestamp || Date.now(),
          with_top: 1
        })
        // 模拟请求失败
        // if (Math.random() > 0.5) {
        //   JSON.parse('dsafafagag')
        // }
        // 2.把请求结果数据放到list数组中
        const { results } = data.data
        this.list.push(...results)
        // 3.本次数据加载状态结束之后要把加载状态设置为结束
        // loading关闭以后才能触发下一次的加载更多
        this.loading = false
        // 4.判断数据是否全部加载完成
        if (results.length) {
          // 更新获取下一页数据的时间戳
          this.timestamp = data.data.pre_timestamp
        } else {
          // 没有数据了，将finished 设置为true
          this.finished = true
        }
      } catch (err) {
        //   展示错误提示状态
        this.error = true
        // 请求失败了，loading也需要关闭
        this.loading = false
      }
    },
    async  onRefresh () {
      // 1. 请求获取数据
      try {
        const { data } = await getArticlesAPI({
          channel_id: this.channel.id, // 频道id
          // 请求数据的页码
          // 下拉刷新，每次请求获取最新数据，传递当前最新时间戳
          timestamp: Date.now(),
          with_top: 1
        })
        // 模拟请求失败
        // if (Math.random() > 0.5) {
        //   JSON.parse('dsafafagag')
        // }
        // 2. 将数据追加到列表的顶部
        const { results } = data.data
        this.list.unshift(...results)
        // 3. 关闭下拉刷新的loading状态
        this.isreFreshLoading = false
        // 4.更新下拉刷新成功提示文本
        this.refreshSuccessText = `刷新成功，更新了${results.length}条数据`
      } catch (err) {
        this.refreshSuccessText = '刷新失败'
        this.isreFreshLoading = false
      }
    }
  }
}
</script>

<style lang="less" scoped>
.article-list {
  height: 80vh;
  overflow-y: auto;
}
</style>
